# -*- coding: utf-8 -*-

import logging.config
from configobj import ConfigObj
from services.bookServer.isbndbClient import IsndbClient
from services.coverServer.librarythingClient import LibrarythingClient

class BookManager(object):
    def __init__(self):
        """
        Book Manager
        """
        self.config = ConfigObj('conf/pyBook.conf')
        log_conf_file = self.config['log_conf_file']

        print log_conf_file
        # Initialize the log
        logging.config.fileConfig(log_conf_file)
        logging.info("Starting BookManager")
        self.book_list = []
        self.iteration = 0
        self.bseeker = IsndbClient()
        self.cseeker = LibrarythingClient()

    def search_by_title(self, title, more_results):
        logging.info("Searching by title %s" % title)
        if not more_results:
            self.book_list = []
        self.book_list = self.book_list + self.bseeker.search_books_by_title(str(title), more_results)
        

    def search_by_author(self, author, more_results):
        logging.info("Searching by author %s" % author)
        if not more_results:
            self.book_list = []
        self.book_list = self.book_list + self.bseeker.search_books_by_author(str(author), more_results)

    def search_by_isbn(self, isbn, more_results):
        logging.info("Searching by ISBN %s" % isbn)
        if more_results:
            return self.book_list
        
        self.book_list = self.bseeker.search_book_by_isbn(str(isbn), more_results)

    def search_by_publisher(self, publisher, more_results):
        logging.info("Searching by publisher %s" % publisher)
        if not more_results:
            self.book_list = []
        self.book_list = self.book_list + self.bseeker.search_books_by_publisher(str(publisher), more_results)

    def search_by_subject(self, subject, more_results):
        logging.info("Searching by title %s" % subject)
        if not more_results:
            self.book_list = []
        self.book_list = self.book_list + self.bseeker.search_books_by_subject(str(subject), more_results)

    def get_book_list(self):
        return self.book_list

    def get_cover(self, book):
        return self.cseeker.get_cover(book)

    def update_view(self):
        self.view.updateView()